package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.product.domain.Brand;
import com.spzx.product.mapper.BrandMapper;
import com.spzx.product.service.IBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-04-28
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements IBrandService {

    /**
     * 根据条件分页查询品牌列表
     * @param name
     * @return
     */
    @Override
    public List<Brand> selectList(String name) {
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(name),Brand::getName, name);
        List<Brand> list = list(wrapper);
        return list;
    }

    /**
     * 根据id获取品牌信息
     * @param id
     * @return
     */
    @Override
    public Brand customGetOne(Long id) {
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Brand::getId, id);
        wrapper.select(Brand::getId, Brand::getName, Brand::getLogo);
        Brand brand = getOne(wrapper);
        return brand;
    }

    /**
     * 添加品牌信息
     * @param brand
     */
    @Override
    public void add(Brand brand) {
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<Brand>();
        wrapper.eq(Brand::getName, brand.getName());
        long count = count(wrapper);
        if (count > 0) {
            throw new ServiceException("商品已存在");
        }
        save(brand);
    }
}
